import 'package:flutter/material.dart';

import 'CustomTextPainter_rederbox.dart';



class GiftBarItem extends StatefulWidget {
  final String text;
  final TextStyle textStyle;

  const GiftBarItem({Key? key, required this.text, required this.textStyle})
      : super(key: key);

  @override
  _GiftBarItemState createState() => _GiftBarItemState();
}

class _GiftBarItemState extends State<GiftBarItem>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      duration: Duration(seconds: 6),
      vsync: this,
    );

    _animation = Tween<Offset>(
      begin: const Offset(100.0, 0.0), // 从屏幕右侧开始
      end: const Offset(-200.0, 0.0), // 移动到屏幕左侧之外
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.linear));

    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        return Transform.translate(
          offset: _animation.value,
          /*    child: Container(
            width: 100,
            height: 40,
            color: Colors.orange,
              child: Center(
                child: Text(widget.text, style: widget.textStyle),
              )

          ),*/
          child: CustomTextPainter(text: "hhhhhhhhhhhhhhhhhhhhh",
              mScreenW: MediaQuery
                  .of(context)
                  .size
                  .width

          ),
        );
      },
    );
  }
}

class GiftAnimationDemo extends StatefulWidget {
  @override
  _GiftAnimationDemoState createState() => _GiftAnimationDemoState();
}

class GiftItem {
  final String name;
  final String message;

  GiftItem(this.name, this.message);
}

class _GiftAnimationDemoState extends State<GiftAnimationDemo> {
  List<GiftItem> gifts = [];

  void _addGift() {
    setState(() {
      gifts.add(GiftItem('User${gifts.length + 1}', '送了一个礼物'));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Gift Animation Demo')),
      body: Stack(
        children: [
          // 主内容区域
          Center(
            child: ElevatedButton(
              onPressed: _addGift,
              child: Text('发送礼物'),
            ),
          ),
          // 礼物飘条显示区域
          Positioned(
            top: 100,
            left: 0,
            right: 0,
            height: 200,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: gifts.map((gift) {
              /*  return Container(child: CustomTextPainter(
                  text: gift.name, textStyle: TextStyle(
                    color: Colors.green
                ), mScreenW: MediaQuery
                    .of(context)
                    .size
                    .width,)
                );*/
                return  CustomTextPainter(
                  text: gift.name, textStyle: TextStyle(
                    color: Colors.green
                ), mScreenW: MediaQuery
                    .of(context)
                    .size
                    .width,);
              })
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: GiftAnimationDemo(),
  ));
}
